/*
 * MachineManager.java
 *
 * Created on 5 de junio de 2007, 08:58
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package wsserver;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.Vector;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlRootElement;
/**
 *
 * @author mariano
 */
@WebService(name="Gpi", serviceName="Gpi", portName="GpiPort")
@SOAPBinding(style=SOAPBinding.Style.RPC)
@XmlRootElement 
public class ComputerManager {
    private Vector<Computer> computers;
    
    /** Creates a new instance of MachineManager */
    
    public ComputerManager() {
        this.setComputers(ComputerManager.getComputersFromFile());
    }
    
    public void inicializaVector(){ //vacio
        this.setComputers(new Vector<Computer>());
    }
    
    public boolean log( String text )
    {
        return log( "log.txt", text );
    }
    
    public boolean log( String filename, String text )
    {
        FileWriter fw = null;
        
        try
        {
            fw = new FileWriter( "log.txt", true);
            fw.write( text );
            System.out.println( text );
            fw.close();
            return true;
        }
        catch( java.io.IOException e )
        {
            System.err.println( "could not log transaction" );
            return false;
        }
    
    }
    
    @WebMethod(operationName="register")
    public void registerMachine( Computer computer )
    {
        
        // si ya esta en la lista solo le cambiamos el status
        if( getComputers().contains( computer ) )
        {
            getComputers().get( getComputers().indexOf( computer ) ).setStatus( computer.getStatus() );
            persist();
            log( "Modifying state of " + computer.getIp().toString() + " to " + computer.getStatus()  );
            
        }
        else
        {
            getComputers().add(computer);
            persist();
            log( "registering " + computer.toString() );
        }
    }



    @WebMethod(operationName="unregister")
    public void unregisterMachine( Computer computer )
    {
        if( getComputers().contains( computer ) )
        {
            getComputers().remove( computer );
            persist();
            log( "unregistering " + computer.toString() );
        }
    }
    
   
    public Vector<Computer> getComputers()
    {
        return computers;
    }
    
     @WebMethod(operationName="getList")
     public String getComputerList(){
        return getXML();
     }

    public void setComputers(Vector<Computer> computers) {
        this.computers = computers;
    }
    
    public void persist(){
        //ver..esto borra lo anterior y pone todo de nuevo..ineficiente
        try {
            JAXBContext context = JAXBContext.newInstance(ComputerManager.class);
            Marshaller m = context.createMarshaller();
            File file = new File("clientslist.xml");
            FileOutputStream outputFile = new FileOutputStream(file);
            m.marshal(this, outputFile);
        }catch(FileNotFoundException ex){}catch(JAXBException e){}
    }
    
    public String getXML(){
        StringWriter writer = new StringWriter();
        
        try {
            JAXBContext context = JAXBContext.newInstance(ComputerManager.class);
            Marshaller m = context.createMarshaller();
            m.marshal(this, writer);
            
            System.out.println(writer);
        }catch(JAXBException e){}
        return writer.toString();
    }
    
    public static Vector<Computer> getComputersFromFile(){
        String listaXML = Util.readTextFile("clientslist.xml");
        StringReader reader = new StringReader(listaXML);
        try{
            JAXBContext context = JAXBContext.newInstance(ComputerManager.class);
            Unmarshaller u = context.createUnmarshaller();
            ComputerManager cm = (ComputerManager) u.unmarshal(reader);
            
            return cm.getComputers();
        }catch(JAXBException e){}
        
        return null;
    }
    
}
